package com.paw.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.paw.file.entity.FileInfo;
import com.tg.paw.common.annotation.SysLog;
import com.tg.paw.common.constant.AllEnum;
import com.tg.paw.common.util.CommonResult;
import com.tg.paw.common.util.NameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.paw.file.service.IFileInfoService;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * 文件信息 前端控制器
 * </p>
 *
 * @author Rubble
 * @since 2020-04-24
 */
@RestController
@Slf4j
@Api(tags = "FileInfoController", value = "文件信息管理")
@RequestMapping("/sys/fileInfo")

public class FileInfoController {
    @Autowired
    private IFileInfoService fileInfoService;


    @SysLog(MODULE = "file", REMARK = "根据条件查询所有文件信息列表")
    @ApiOperation("根据条件查询所有文件信息列表")
    @GetMapping(value = "/")
    public Object getFileInfoByPage(FileInfo entity,
                                    @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "sortField", defaultValue = "") String sortField,
                                    @RequestParam(value = "sortOrder", defaultValue = "") String sortOrder) {
        try {
            QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>(entity);
            if(StringUtils.isNotEmpty(sortField) && StringUtils.isNotEmpty(sortOrder)){
                sortField = NameUtil.humpToLine(sortField);
                if(AllEnum.SortEnum.ASC.desc().equals(sortOrder)){
                    queryWrapper.orderByAsc(sortField);
                }else {
                    queryWrapper.orderByDesc(sortField);
                }
            }
            return CommonResult.success(fileInfoService.page(new Page<FileInfo>(pageNo, pageSize), queryWrapper));
        } catch (Exception e) {
            log.error("根据条件查询所有文件信息：{}", e.getMessage(), e);
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "file", REMARK = "保存文件信息")
    @ApiOperation("保存文件信息表")
    @PutMapping(value = "/")
    public Object saveFileInfo(@RequestBody FileInfo entity) {
            try {
            if (fileInfoService.save(entity)) {
            return CommonResult.success();
            }
            } catch (Exception e) {
            log.error("保存文件信息表：{}", e.getMessage(), e);
            return CommonResult.failed();
            }
            return CommonResult.failed();
    }

    @SysLog(MODULE = "file", REMARK = "更新文件信息")
    @ApiOperation("更新文件信息")
    @PostMapping(value = "/{id}")
    public Object updateFileInfo(@RequestBody FileInfo entity,@ApiParam("文件信息id") @PathVariable Long id) {

        try {
            entity.setId(id);
            if (fileInfoService.updateById(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("更新文件信息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "file", REMARK = "删除文件信息")
    @ApiOperation("删除文件信息")
    @DeleteMapping(value = "/{id}")
    public Object deleteFileInfo(@ApiParam("文件信息id") @PathVariable Long id) {
        try {
            if (fileInfoService.removeById(id)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("删除文件信息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "file", REMARK = "查询文件信息")
    @ApiOperation("查询文件信息")
    @GetMapping(value = "/{id}")
    public Object getFileInfoById(@ApiParam("文件信息id") @PathVariable Long id) {
        try {
            FileInfo coupon = fileInfoService.getById(id);
            return CommonResult.success(coupon);
        } catch (Exception e) {
            log.error("查询文件信息：{}", e.getMessage(), e);
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量删除文件信息")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    @SysLog(MODULE = "file", REMARK = "批量文件信息")
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean count = fileInfoService.removeByIds(ids);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
